package com.example.venadaoa.tool.imagechoose;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by Dell on 2016/8/1.
 */
public class ImageLayoutManager extends GridLayoutManager {
    private int lineCount =0;
    public ImageLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
        this.lineCount =spanCount;
    }

    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
        int itemCount = state.getItemCount() ;
        if(itemCount == 0) {
            super.onMeasure(recycler, state, widthSpec, heightSpec);
            return ;
        }
        View view = recycler.getViewForPosition(0);
        if(view != null){
            measureChild(view, widthSpec, heightSpec);
            int measuredWidth = View.MeasureSpec.getSize(widthSpec);
            int measuredHeight = view.getMeasuredHeight();

            if(itemCount >lineCount &&itemCount<=lineCount*2) {
                setMeasuredDimension(measuredWidth, measuredHeight*2);
            }else if(itemCount > lineCount*2)
            {
                setMeasuredDimension(measuredWidth, measuredHeight*3);
            }else
            {
                setMeasuredDimension(measuredWidth, measuredHeight);
            }
        }
    }
}
